package cn.lll.vuehouse.controller;


import cn.lll.vuehouse.DTO.FileDTO;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.Allfile;
import cn.lll.vuehouse.service.AllfileService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RequestMapping("file")
@RestController
public class FileController {

    @Autowired
    private AllfileService allfileService;



    //文件上传接口
    @PostMapping("upload")
    @ResponseBody
    public ResponseResult upload(@RequestParam("file")MultipartFile file){
        return allfileService.upload(file);
    }


    //展示
    @GetMapping("page")
    public ResponseResult getPage(FileDTO fileDTO){

        return allfileService.getPage(fileDTO);
    }


    //文件下载接口
    @GetMapping("down")
    public  void down(String fileName, HttpServletResponse response){
        allfileService.down(fileName,response);
    }


    @GetMapping("getById")
    public ResponseResult getById(Integer id){
        ResponseResult responseResult = new ResponseResult();
        Allfile allfile = allfileService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS,allfile);
        return responseResult;
    }


    //删除文件
    @DeleteMapping("deleteById")
    public ResponseResult deleteById(Integer id){
        ResponseResult responseResult = new ResponseResult();
        Allfile allfile = allfileService.getById(id);
        allfileService.removeById(id);
        String uu = allfile.getUuidName();
        File file = new File("D:\\upload\\"+uu);
        file.delete();


        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

}
